<?php
/*
$JA#COPYRIGHT$
*/

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );
require_once(JPATH_SITE.DS.'components'.DS.'com_jajobboard'.DS.'helper'.DS.'jafunctions.class.php');

class JElementLocation extends JElement
{
	/*
	 * Element name
	 *
	 * @access	protected
	 * @var		string
	 */
	var	$_name = 'Fields'; 

	function fetchElement($name, $values, &$node, $control_name)
	{
	    $db =& JFactory::getDBO();
		$temp = JRequest::get('request');
		$options = $this->getFields();	
		$object_name = $control_name.'['.$name.']';
		$result = '<select name="'.$object_name .'[]">';
		$salary_checked = '';
		if (!is_array($values))
			$values = explode(',',$values);
		
	    $query = "SELECT * FROM #__ja_location where published = 1";
		$db->setQuery ( $query );
		$objects = $db->loadObjectList ();
		
		$objectHTML = array ();
		$children = array ();
		if ($objects) {
			foreach ( $objects as $v ) {
				$pt = $v->parent_id;
				$list = @$children [$pt] ? $children [$pt] : array ();
				array_push ( $list, $v );
				$children [$pt] = $list;
			}
			if (is_array ( $children )) {
				
				foreach ( $children as $key => $value ) {
					$temp = array ();
					$tmp_children = array ();
					if (is_array ( $children [$key] )) {
						foreach ( $children [$key] as $k => $v )
							$temp [$k] = $v->name;
						asort ( $temp );
						foreach ( $temp as $k => $v )
							$tmp_children [$k] = $children [$key] [$k];
						$children [$key] = $tmp_children;
					}
				}
			
			}
			
			$list = $this->treerecurse ( 0, '', array (), $children );
			if ($list) {
				foreach ( $list as $i => $item ) {
					$treename = $item->treename;
					$treename = JFilterOutput::ampReplace ( $treename );
					$treename = str_replace ( '"', '&quot;', $treename );
					if ($item->id == 0)
						$list [$i]->treename = $treename;
					else
						$list [$i]->treename = $treename;
				}
			}
			$objects = $list;
		}
		if (is_array($objects))
		foreach ( $objects as $key => $v ) {
			$objectHTML [] = JHTML::_ ( 'select.option', $key, $v->treename );
		}
		
		$strReturn = JHTML::_('select.genericlist',   $objectHTML, $object_name .'[]', 'class="inputbox" size="1"', 'value', 'text', $values);
		
		return $strReturn;
	}
	
	function treerecurse($id, $indent, $list, &$children, $maxlevel = 9999, $level = 0, $type = 1) {
    	if (@$children [$id] && $level <= $maxlevel) {
    		if (is_array ( $children ))
    			foreach ( $children [$id] as $v ) {
    				$id = $v->id;
    				
    				if ($type) {
    					$pre = '<sup>|_</sup>&nbsp;';
    					$spacer = '.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
    				} else {
    					$pre = '- ';
    					$spacer = '&nbsp;&nbsp;';
    				}
    				
    				if ($v->parent_id == 0) {
    					$txt = $v->name;
    				} else {
    					$txt = $pre . $v->name;
    				}
    				$pt = $v->parent_id;
    				$list [$id] = $v;
    				$list [$id]->treename = "$indent$txt";
    				$list [$id]->children = count ( @$children [$id] );
    				$list = $this->treerecurse ( $id, $indent . $spacer, $list, $children, $maxlevel, $level + 1, $type );
    			}
    	}
    	
    	return $list;
    }
	
	function getFields()
	{
		$db =& JFactory::getDBO();
		
		$query = 'SELECT f.id, f.name '
		. ' FROM #__ja_location AS f '
		. " WHERE f.published=1 ORDER BY f.name ASC"
		;
		$db->setQuery( $query );
		
		$options = $db->loadObjectList();		
		return $options;
	}
	
}
